﻿#region Imports
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using CommonUtilities.Controls;
using CommonUtilities.Extensions;
using DirectoryAnalyzer.Controls.ViewModels;
#endregion

namespace DirectoryAnalyzer.Controls
{
    /// <summary>
    /// ToggleButton
    /// </summary>
    public partial class GraphToggleButton : UserControl
    {
        #region Declarations
        // RKD
        ///// <summary>
        ///// Delegate for handling toggle state change event
        ///// </summary>
        ///// <param name="sender">RKD</param>
        ///// <param name="e">RKD</param>
        //public delegate void ToggleStateChangedEventHandler(object sender, RoutedEventArgs e);

        /// <summary>
        /// EventHandler for handling toggle state change
        /// </summary>
        public event EventHandler ToggleStateChangedEventHandler;
        public event EventHandler MouseEnter;
        public event EventHandler MouseLeave;
        #endregion

        #region Properties
        private FontWeight InitFontWeight { get; set; }
        #endregion

        #region Constructors
        public GraphToggleButton()
        {
            InitializeComponent();
            txtToggleText.FontFamily = new FontFamily(CUtil_Button.DefaultFont.FontFamily.Name);

            if (!tglToggleButton.DataContext.IsNull())
                imgDisplayGraph.Source = ((ToggleButtonViewModel)tglToggleButton.DataContext).GraphImageSource;
        }
        #endregion

        #region Events
        private void tglToggleButton_CheckedStateChanged(object sender, RoutedEventArgs e)
        {
            if (this.ToggleStateChangedEventHandler != null) this.ToggleStateChangedEventHandler(sender, e);
        }

        private void tglToggleButton_MouseEnter(object sender, MouseEventArgs e)
        {
            ToggleButton btn = (ToggleButton)sender;
            InitFontWeight = btn.FontWeight;
            btn.FontWeight = FontWeights.Bold;

            if (!this.MouseEnter.IsNull())
                this.MouseEnter(sender, e);
        }

        private void tglToggleButton_MouseLeave(object sender, MouseEventArgs e)
        {
            ToggleButton btn = (ToggleButton)sender;
            btn.FontWeight = InitFontWeight;

            if (!this.MouseLeave.IsNull())
                this.MouseLeave(sender, e);
        }
        #endregion

        #region Methods
        internal void UpdateCheckState(bool isChecked)
        {
            if (!tglToggleButton.DataContext.IsNull())
                ((ToggleButtonViewModel)tglToggleButton.DataContext).IsToggleChecked = isChecked;
        }

        internal bool GetCheckedState()
        {
            if (!tglToggleButton.DataContext.IsNull())
                return ((ToggleButtonViewModel)tglToggleButton.DataContext).IsToggleChecked;
            else return false;
        }

        internal void UpdateGraphImage(string imgRelPath)
        {
            // RKD
            //https://msdn.microsoft.com/en-us/library/system.windows.controls.image.source%28v=vs.100%29.aspx
            //http://stackoverflow.com/questions/18625086/how-to-set-image-source-in-c-sharp-to-xaml-static-resource-programmatically
            //http://stackoverflow.com/questions/15377431/set-image-source-from-resources-in-wpf-application
            //http://stackoverflow.com/questions/350027/setting-wpf-image-source-in-code
            //http://stackoverflow.com/questions/3787137/change-image-source-in-code-behind-wpf
            //https://social.msdn.microsoft.com/forums/vstudio/en-US/35a38027-2289-4551-92bb-313b5adce403/set-wpf-image-source-programmatically
            //http://www.codeproject.com/Questions/445685/How-To-Set-Image-Source-runtime-in-wpf
            //
            //BitmapImage imgSource = new BitmapImage(new Uri(imgRelPath, UriKind.Relative));
            if (!tglToggleButton.DataContext.IsNull())
                ((ToggleButtonViewModel)tglToggleButton.DataContext).GraphImageSource = (ImageSource)new ImageSourceConverter().ConvertFromString(imgRelPath); // imgSource;
        }
        #endregion
    }
}
